<?php
/**
 * Created by PhpStorm.
 * User: yangyou
 * Date: 2018/12/3 0003
 * Time: 10:48
 * 建房知识
 */

namespace app\index\controller;


use app\index\model\Cases;
use app\index\model\Drawing;
use app\index\model\FriendshipLink;
use app\index\model\Information;
use app\index\model\InformationClassification;
use app\index\model\InformationKeyword;
use app\index\model\Designer;

class News extends Base
{
    /**
     * Author:yang you
     * Time:2018-12-15 10:00:43
     * Describe:资讯聚合页
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index($key="")
    {
        $informationClassification = new InformationClassification();
        $information = new Information();
        $drawing = new Drawing();
        $case = new Cases();
        $classification_id = "";
        //今日推荐
        $todayNews = $information->todayOrHotNews($type="1");
        //热门推荐
        $hotNews = $information->todayOrHotNews($type="2");
        //资讯类型
        $informationType = $informationClassification->informationClassificationList($parent_id="",$is_index="1");
        //子分类
        foreach ($informationType as $key=>$val){
            $informationType[$key]['informationList'] = $information->informationList($val['classification_id']);//下面四款最热门的别墅图片
        }

        //下面四款最热门的别墅图片
        $drawingList = $drawing->drawingList($keyword = "",$style = "",$layct = "",$structure="",$house="",$is_top="1",$orders="",$after_limit="16");
        //右侧三款随机图纸
        $leftCase = $case->leftCaseList();
        //热门标签
        $hotTag =  $information->randKeywordInfo();
        //友情链接
        $FriendShipLink = new FriendshipLink();
        $linkList = $FriendShipLink->friendshipLinkList();
        return view('news/index',[
            'todayNews' => $todayNews,
            'hotNews' => $hotNews,
            'informationType' => $informationType,
            'drawingList' => $drawingList,
            'leftcase' => $leftCase,
            'hotTag' => $hotTag,
            'linkList' => $linkList
        ]);

    }

    /**
     * Author:yang you
     * Time:2018-11-15 10:00:43
     * Describe:建房知识列表
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function newslist($key="")
    {
        $informationClassification = new InformationClassification();
        $information = new Information();
        $case = new Cases();
        $classification_id = '';
        $newsClassification = $informationClassification->informationClassificationList($parent_id="",$is_index="1");
        $seo_info = '';
        //资讯的子分类
        foreach ($newsClassification as $val){
            if($val["py"] == $key){
                $classification_id = $val["classification_id"];
                $seo_info["seo_title"] =  $val["description"];
                $seo_info["seo_keyword"] =  $val["seo_keyword"];
                $seo_info["seo_description"] =  $val["seo_description"];
                break;
            }
        }
        $newslist= $information->newsList($classification_id);
        //右侧三款随机图纸
        $rightcase = $case->leftCaseList();
        //热门标签
        $hotTag =  $information->randKeywordInfo();
        return view('news/newslist',[
            'newsClassification' => $newsClassification,
            'newslist' => $newslist,
            'page' => $newslist->render(),
            'rightcase' => $rightcase,
            'hotTag' => $hotTag,
            'seo_info' => $seo_info
        ]);

    }

    /**
     * Author:yang you
     * Time:2018-11-15 10:00:43
     * Describe:建房知识详情
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function newsDetail()
    {
        $information_id = input("information_id/d");
        $information = new Information();
        $designer = new Designer();
        $case = new Cases();
        $newsInfo = $information->info($information_id);
        if(empty($newsInfo)){
            abort(404,'页面不存在');
        }
        //跟新阅读量
        $information->setFieldValue('information_id',$information_id,'reading_number',$newsInfo['reading_number']+1);
        //设计师随机获取
        $designerInfo = $designer->randInfo();
        //右侧三款随机图纸
        $rightcase = $case->leftCaseList();
        //栏目右边8篇文章
        $informationList = $information->informationList($classification_id="",$limit="8");
        return view('news/newsDetail',[
            'newsInfo' => $newsInfo,
            'designerInfo' => $designerInfo,
            'rightcase' => $rightcase,
            'informationList' => $informationList
        ]);

    }

    /**
     * Author:yang you
     * Time:2018-11-15 10:00:43
     * Describe:关键字搜索页
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function keywordResult($key)
    {
        //拿取关键字的名字
        $keyInfo = explode('-',$key);
        $information_id = $keyInfo[0];//新闻ID
        $key_serial = $keyInfo[1];//新闻里面关键字的序号
        $information = new Information();
        $drawing = new Drawing();
        $case = new Cases();
        $newsInfo = $information->info($information_id);
        $keywords = $newsInfo["keywords"];
        $keyword = $keywords[$key_serial-1];//目标关键字（新闻ID）-（新闻里面关键字的序号）
        //匹配的三款图纸
        $drawingList = $drawing->drawingList($keyword,$style = "",$layct = "",$structure="",$house="",$is_top="",$orders="",$after_limit="3");
        //匹配的图纸定制案例
        $caseList =  $case->caseList($style="",$layct="",$is_top="",$classification_id="12",$structure="",$orders="",$after_limit = "3",$keyword);
        //根据关键字匹配的5篇文章
        $newsList = $information->newsList($classification_id="",$limit="5",$keyword);
        //热门标签
        $hotTag = $information->randKeywordInfo();
        //右侧定制案例
        $rightcase = $case->leftCaseList();
        //栏目右边8篇文章
        $informationList = $information->informationList($classification_id="",$limit="8");
        $seo_info['seo_title'] =$keyword.'-别墅工场';
        $seo_info['seo_keyword'] = $keyword;
        $seo_info['seo_description'] = '';
        //相关关键字
        $keywordInfo = $information->relateKeyword($keyword);
        $keyResult = $this->unique_array($keywordInfo,$keyword);
        return view('news/keywordResult',[
            'drawingList' => $drawingList,
            'caseList' =>$caseList,
            'rightcase' => $rightcase,
            'informationList' => $informationList,
            'newsList' => $newsList,
            'keyword' => $keyword,
            'hotTag' => $hotTag,
            'seo_info' => $seo_info,
            'keyResult' => $keyResult
        ]);
    }

    /**
     * Author:yang you
     * Time:2018-11-15 10:00:43
     * Describe:头部栏目的搜索
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function search()
    {
        $information = new Information();
        $drawing = new Drawing();
        $case = new Cases();
        $keyword_name = input("keyword_name");
        if(!empty($keyword_name)){
            session("keyword_name",$keyword_name);//将存储的关键字存起来
            return $keyword_name;
        }else{
            $keyword_name = session("keyword_name");
            //匹配的三款图纸
            $drawingList = $drawing->drawingList($keyword_name,$style = "",$layct = "",$structure="",$house="",$is_top="",$orders="",$after_limit="3");
            //匹配的图纸定制案例
            $caseList =  $case->caseList($style="",$layct="",$is_top="",$classification_id="12",$structure="",$orders="",$after_limit = "3",$keyword_name);
            //根据关键字匹配的5篇文章
            $newsList = $information->newsList($classification_id="",$limit="5",$keyword_name);
            //热门标签
            $hotTag = $information->randKeywordInfo();
            //右侧定制案例
            $rightcase = $case->leftCaseList();
            //栏目右边8篇文章
            $informationList = $information->informationList($classification_id="",$limit="8");
            $seo_info['seo_title'] =$keyword_name.'-别墅工场';
            $seo_info['seo_keyword'] = $keyword_name;
            $seo_info['seo_description'] = '';
            //相关关键字
            $keywordInfo = $information->relateKeyword($keyword_name);
            $keyResult = $this->unique_array($keywordInfo,$keyword_name);
            return view('news/keywordResult',[
                'drawingList' => $drawingList,
                'caseList' =>$caseList,
                'rightcase' => $rightcase,
                'informationList' => $informationList,
                'newsList' => $newsList,
                'keyword' => $keyword_name,
                'hotTag' => $hotTag,
                'seo_info' => $seo_info,
                'keyResult' => $keyResult
            ]);
        }
    }

    //筛选去掉重复的数组
    function unique_array($keywordInfo,$keyword_name){
        $result = [];
        if(!empty($keywordInfo)){
            foreach ($keywordInfo as $val){
                foreach ($val["keywords"] as $key=>$info){
                    $key++;
                    if(strstr($info,$keyword_name)){
                        $result[$val["information_id"].'-'.$key] = $info;
                    }
                }
            }
        }
        $result = array_unique($result);
        return $result;
    }
}